﻿using ControlCore;
using ControlCore.Device;
using System;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;

namespace BErPiDeviceV1
{
    [Export(typeof(IDevice))]
    [ExportMetadata("Name", "BErPiV1 device")]
    public class BErPiDeviceV1 : IDevice, IDisposable
    {
        public FrameworkElement DeviceOutput { get; private set; }

        public bool IsInitialized { get; private set; }

        #region ctor

        public void Dispose()
        {
            if (DeviceOutput is WebBrowser)
            {
                (DeviceOutput as WebBrowser).Dispose();
            }
        }

        #endregion

        public void Init()
        {
            DeviceOutput = new WebBrowser();
            (DeviceOutput as WebBrowser).Source = new System.Uri("http://192.168.0.7:8080/javascript_simple.html");
        }

        public void Close()
        {
        }


        public void SendCommand(ControlCommand deviceCommand, double value)
        {
            var wsCommunicator = new WebServiceCommunicator();
            var result = string.Empty;
            try
            {
                result = wsCommunicator.TestWS("{\"command\":\"" + deviceCommand.ToString() + "\", \"value\":\"" + value.ToString() + "\"}");
            }
            catch (Exception wsce)
            {
                result = wsce.Message;
            }

            MessageBox.Show(result);
        }
    }
}
